/*
 * @Author: 彭林
 * @Date: 2021-05-13 19:29:51
 * @LastEditTime: 2021-07-01 15:57:18
 * @LastEditors: 彭林
 * @Description:
 * @FilePath: /src/saas-app/app/api/v1/zuowen.go
 */
package v1

import (
	"saas-app/app/model"
	"saas-app/app/service"
	"saas-app/library/cache"
	"saas-app/library/response"

	"github.com/gogf/gf/net/ghttp"
)

func SaveZuowen(r *ghttp.Request) {

	var zuowenModel model.Zuowen

	if err := r.Parse(&zuowenModel); err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	if err := service.ZuowenService().CreateZuowen(&zuowenModel); err != nil {
		response.JsonExit(r, 1, "同步失败")
	} else {
		response.JsonExit(r, 0, "同步成功", nil)
	}
}

func GetZuowenList(r *ghttp.Request) {
	cache.Get(r, r.RequestURI)
	page := r.GetQueryInt("page")
	limit := r.GetQueryInt("limit")

	result, total := service.ZuowenService().GetZuowenList(page, limit)
	var resp = response.Paging(result, total, page, limit)
	cache.Set(r.RequestURI, resp, 60)
	response.JsonExit(r, 0, "作文列表", resp)
}

func GetZuowenLastId(r *ghttp.Request) {
	id := service.ZuowenService().GetZuowenLastId()
	response.JsonExit(r, 0, "文章id", id)
}

func GetZuowenInfo(r *ghttp.Request) {
	zuowenID := r.GetFormUint("zuowen_id")
	data := service.ZuowenService().GetZuowenInfo(zuowenID)
	response.JsonExit(r, 0, "作文详情", data)
}
